#!/bin/bash
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

#
# Copyright (c) 2014, Joyent, Inc.
#

#
# Run the TODONAME tests.
# Run `./runtests -h` for usage info.
#

if [ "$TRACE" != "" ]; then
    export PS4='${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
    set -o xtrace
fi
set -o errexit
set -o pipefail



#---- guard

if [[ ! -f "/lib/sdc/.sdc-test-no-production-data" ]]; then
    cat <<EOF
To run this test you must create the file:

    /lib/sdc/.sdc-test-no-production-data

after ensuring you have no production data on this SDC.
EOF
    exit 2
fi



#---- config

NAME=TODONAME
TOP=$(cd $(dirname $0)/../; pwd)



#---- support functions

function fatal
{
    echo "$(basename $0): fatal error: $*"
    exit 1
}

function usage
{
    echo "Usage:"
    echo "  runtests [OPTIONS...]"
    echo ""
    echo "Options:"
    echo "  -f FILTER   Filter pattern (substring match) for test files to run."
}



#---- mainline

start_time=$(date +%s)

# Options.
opt_test_pattern=
while getopts "hf:" opt
do
    case "$opt" in
        h)
            usage
            exit 0
            ;;
        f)
            opt_test_pattern=$OPTARG
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done

OUTPUT_DIR=/var/tmp/${NAME}test
echo "# Create output dir ($OUTPUT_DIR)."
rm -rf $OUTPUT_DIR
mkdir -p $OUTPUT_DIR



#---- start tests

# TODO
# Project specific setup and test running goes here:
#
# - Rule #1: exit non-zero if any tests failed. This is most easily done
#   by running the test driver with all test files as the last command
#   (assuming the test driver isn't broken -- see RFD 139). For example:
#       $(TAP_EXEC) --jobs=$(TEST_JOBS) \
#               --output-file=$(OUTPUT_DIR)/test.integration.tap \
#               test/integration/**/*.test.js
#
# - TAP output should be redirected or tee'd to $OUTPUT_DIR/*.tap
#   This is used (a) to summarize below and (b) for the Jenkins Job TAP
#   parsing/reporting.
#
# - If reasonable, use $opt_test_pattern to filter the set of test files run.
#
# Some examples:
# - https://github.com/joyent/sdcadm/blob/master/test/runtests

retval=0
exit $retval
